package com.goulin.sprmvc.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.goulin.sprmvc.converter.MyMessageConverter;
import com.goulin.sprmvc.interceptor.DemoInterceptor;

@Configuration//这是一个设置类
@EnableWebMvc//开启mvc模式
@ComponentScan("com.goulin.sprmvc")//扫描包
public class MyMvcConfig extends WebMvcConfigurerAdapter{//继承此类
	
	@Bean
	public InternalResourceViewResolver viewResolver(){
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/classes/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
		return viewResolver;
	}
	@Bean
	public MultipartResolver multipartResolver(){
		CommonsMultipartResolver cmr = new CommonsMultipartResolver();
		cmr.setMaxUploadSize(1000000);
		return cmr;
	}
	
	
	//静态资源设置，不会出现rest方式请求了静态资源
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
	}

	public DemoInterceptor demoInterceptor(){
		return new DemoInterceptor();
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
//		registry.addInterceptor(demoInterceptor()).addPathPatterns("/demo/**");//通过addPathPatterns可以指定需要拦截的路径
		//全部拦截
		registry.addInterceptor(demoInterceptor());
	}

	//设置默认的请求地址
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub
		super.addViewControllers(registry);
		registry.addViewController("/index").setViewName("main");
		registry.addViewController("/demo/index").setViewName("index");
		registry.addViewController("/toUpload").setViewName("upload");
		registry.addViewController("/convert").setViewName("converter");
		registry.addViewController("/sse").setViewName("sse");
	}


	@Override
	public void configurePathMatch(PathMatchConfigurer configurer) {
		super.configurePathMatch(configurer);
		configurer.setUseRegisteredSuffixPatternMatch(Boolean.FALSE);//对于http://*****.zz这样的路径.zz也是会被作为参数收纳进来的
	}
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(converter());
	}
	@Bean
	public MyMessageConverter converter(){
		return new MyMessageConverter();
	}
	
	
}
